/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkgMetodoPyramid.pkgPersistencia;

import java.io.File;
import java.io.IOException;
import pkgMetodoPyramid.pkgNegocio.EntityPyramid;
import pkgMetodoPyramid.pkgNegocio.ReqRespPyramid;
import pkgUtil.PASConstants;
import pkgUtil.pkgSupportFile.PASFile;
import pkgUtil.pkgSupportFile.ReadPASFileException;
import pkgUtil.pkgSupportFile.SavePASFileException;
import pkgUtil.pkgSupportFile.pkgFrameworkFile.ManagerFile;
import pkgUtil.pkgSupportFile.pkgFrameworkFile.Register;

/**
 *
 * @author Administrador
 */
public class PASPyramidFile extends PASFile {

    private ReqRespPyramid reqRespPyramid;
    private HeadDataPASPyramidFile headData;

    public PASPyramidFile(File fileSelect) {
        super(fileSelect, new EntityPyramid(), new HeadDataPASPyramidFile());
    }

    /**
     * @return the reqRespPyramid
     */
    public ReqRespPyramid getReqRespPyramid() {
        return reqRespPyramid;
    }

    /**
     * @param reqRespPyramid the reqRespPyramid to set
     */
    public void setReqRespPyramid(ReqRespPyramid reqRespPyramid) {
        this.reqRespPyramid = reqRespPyramid;
    }

    @Override
    public void save() throws SavePASFileException {
        try {
            ManagerFile mgPas = getInstanceManagerFileForSave();
            mgPas.setHeadDatos(getHeadData());
            for (int i = 0; i < reqRespPyramid.getCountEntity(); i++) {
                EntityPyramid entity = reqRespPyramid.getEntityPyramid(i);
                mgPas.altaDirecta(new Register(entity));
            }

            rename(formatNameFile(), mgPas);
        } catch (Exception ex) {
            throw new SavePASFileException(ex.getMessage());
        }
    }

    @Override
    public void read() throws ReadPASFileException {
        try {
            ManagerFile mgPas = getInstanceManagerFileForRead();
            ReqRespPyramid reqResp = new ReqRespPyramid();

            readHead(mgPas);

            while (!mgPas.eof()) {
                Register reg = mgPas.leer();
                reqResp.addEntityPyramid((EntityPyramid) reg.getData());
            }
            mgPas.close();
            setReqRespPyramid(reqResp);
        } catch (Exception ex) {
            throw new ReadPASFileException(ex.getMessage());
        }
    }

    private void readHead(ManagerFile mgPas) throws InstantiationException, IllegalAccessException, IOException {
        Register reg = mgPas.leerHead();
        headData = (HeadDataPASPyramidFile) reg.getData();
    }

    public boolean validForSave() {
        if (!super.valid()) {
            return isValid();
        }

        if (reqRespPyramid == null || reqRespPyramid.getCountEntity() == 0) {
            this.setErrorValid("No existen EntityPyramid");
            this.setValid(false);
        }

        return isValid();
    }

    public boolean validForRead() {
        return super.valid();
    }

    public String formatNameFile() {
        String pathFile = getFileSelect().getPath();
        String newPathFile;
        String extension = "." + PASConstants.FILE_EXTENSION[0];
        if (pathFile.endsWith(extension)) {
            newPathFile = pathFile.replace(extension, " ");
        } else {
            newPathFile = pathFile;
        }

        return (newPathFile.trim()+extension);
    }

    /**
     * @return the headData
     */
    public HeadDataPASPyramidFile getHeadData() {
        return headData;
    }

    /**
     * @param headData the headData to set
     */
    public void setHeadData(HeadDataPASPyramidFile headData) {
        this.headData = headData;
    }
}
